package com.piotrs10.bsk.gui.views;

import java.util.ArrayList;
import java.util.List;

import com.vaadin.ui.Button;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.themes.ChameleonTheme;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

public class MainView extends VerticalLayout implements Command {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8709590024266551789L;
	private final String ACTION_MENU_CAPTION = "Akcja";
	private final String SHOW_PRIVILIGES_CAPTION = "Pokaz Uprawnienia";
	private final String LOGOUT_CAPTION = "Wyloguj";
	private final String UPDATE_CAPTION = "Zmodyfikuj";
	private final String DELETE_CAPTION = "Usun";
	private final String INSERT_CAPTION = "Dodaj";

	private MenuBar topMenuBar;
	private MenuItem actionMenuItem;
	private MenuItem showPriviligesItem;
	private MenuItem logoutItem;
	private Panel mainPanel;
	private Panel tabsPanel;
	private Panel buttonsPanel;
	private Button updateButton;
	private Button deleteButton;
	private Button insertButton;
	private List<Table> tablesList;
	private TabSheet tabSheet;

	public MainView() {
		super();
		this.setMargin(true);
		this.mainPanel = new Panel();
		this.setSpacing(true);
		this.tabsPanel = new Panel();
		this.tabsPanel.setStyleName(ChameleonTheme.PANEL_BORDERLESS);
		this.buttonsPanel = new Panel();
		this.buttonsPanel.setStyleName(ChameleonTheme.PANEL_BORDERLESS);
		this.updateButton = new Button(this.UPDATE_CAPTION);
		this.deleteButton = new Button(this.DELETE_CAPTION);
		this.insertButton = new Button(this.INSERT_CAPTION);
		HorizontalLayout buttonsLayout = new HorizontalLayout();
		buttonsLayout.setSpacing(true);
		this.buttonsPanel.setContent(buttonsLayout);
		this.buttonsPanel.addComponent(this.insertButton);
		this.buttonsPanel.addComponent(this.updateButton);
		this.buttonsPanel.addComponent(this.deleteButton);
		// gorne menu
		this.topMenuBar = new MenuBar();
		this.actionMenuItem = this.topMenuBar.addItem(this.ACTION_MENU_CAPTION,
				null);
		this.showPriviligesItem = this.actionMenuItem.addItem(
				this.SHOW_PRIVILIGES_CAPTION, this);
		this.logoutItem = this.actionMenuItem
				.addItem(this.LOGOUT_CAPTION, this);
		//taby dolne
		this.tablesList = new ArrayList<Table>();
		VerticalLayout tabsPanelLayout = new VerticalLayout();
		tabsPanelLayout.setSpacing(true);
		this.tabSheet = new TabSheet();
		this.loadTables();
		this.tabsPanel.addComponent(this.tabSheet);
		//dodanie elementow do ramki
		this.addComponent(this.topMenuBar);
		this.addComponent(mainPanel);
		this.mainPanel.addComponent(this.buttonsPanel);
		this.mainPanel.addComponent(this.tabsPanel);
	}

	private void loadTables() {
		// TODO: Odwolanie do DAO w celu sciagniecia tabel
		String names[] = new String[] {"aaa", "bbb", "ccc", "ddd"};
		for(int i = 0; i < names.length; i++) {
			Table table = new Table();
			table.addContainerProperty("Name", String.class, null);
			table.addContainerProperty("Surname", String.class, null);
			table.addContainerProperty("Other", String.class, null);
			for(int j = 0; j < 10; j++) {
				table.addItem(new String[] {"ccc", "ddd", "eee"}, j);
			}
			table.setSelectable(true);
			table.setStyleName(ChameleonTheme.TABLE_STRIPED);
			this.tablesList.add(table);
			this.tabSheet.addTab(table, names[i]);
		}
	}

	@Override
	public void menuSelected(MenuItem selectedItem) {
		if (selectedItem == this.showPriviligesItem) {
			// TODO: Wyswietlenie okna z uprawnieniami
		} else if (selectedItem == this.logoutItem) {
			this.getWindow().setContent(new LoginView());
		}
	}
}
